<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="group.jsp"%>
<%@ include file="/common/task.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
	DoctorGroup doctorGroup = DoctorGroup.fromMap(parameters, DoctorGroup.class);
	String groupName = doctorGroup.getGroupName();
	if (isEmpty(groupName)) {
		msg = new Message(30061, "请设置分组名称");
		Tools.encryptPrint(out, msg);
		return;
	}
	if (groupName.contains(" ")) {
		// msg = new Message(30061, "分组名称不能包含空格");
		// Tools.encryptPrint(out, msg);
		// return;
	}
	doctorGroup.setGroupName(filterIllegalCharacter(groupName));
	doctorGroup.setContent(filterIllegalCharacter(doctorGroup.getContent()));
	if (doctorGroup.getGroupName() == null || doctorGroup.getGroupName().length() == 0 || doctorGroup.getGroupName().length() > 8) {
		msg = new Message(30061, "分组名称1-8个字符");
		Tools.encryptPrint(out, msg);
		return;
	}
	if (doctorGroup.getContent() != null && doctorGroup.getContent().length() > 8) {
		msg = new Message(30061, "分组备注0-8个字符");
		Tools.encryptPrint(out, msg);
		return;
	}
	doctorGroup.setDoctorUid(token.getUid());
	List<DoctorGroup> groups = QBuilder.db("basedb").select().from("DoctorGroup").where(new Where("doctorUid=", token.getUid()).and("status = ", STATUS_ENABLE).and("hospital =", doctorGroup.getHospital()).and("groupName=", doctorGroup.getGroupName())).query().getResultObj();
	if (groups.size() != 0) {
		msg = new Message(30061, "分组已存在");
		Tools.encryptPrint(out, msg);
		return;
	}
	Integer id = (Integer)DBTools.db("basedb").addItem(doctorGroup);
	if (isEmpty(id)) {
		msg = new Message(30061, "创建分组失败");
	} else {
		doctorGroup.setDoctorUid(token.getUid());
		String uids = input.getDesc("uids", "");
		String[] _uids = uids.split("\\|");
		if (!isEmpty(_uids) && !isEmpty(uids)) {
			MyTask task = new MyTask(_uids.length);
			for (int i = 0; i < _uids.length; i++) {
				GroupPatient groupPatient = new GroupPatient();
				groupPatient.setHospital(hospital);
				groupPatient.setGroupId(id);
				task.runTask(task.new MyRunnable(task.new DBOperate() {
					@Override
					public Object operate(Object... objects) {
						List<Integer> userIds = new ArrayList<Integer>();
						Integer patientUid = Tools.getInt(objects[0]+"", 0);
						userIds.add(patientUid);
						Map<Integer, User> users = usersByUids(userIds);
						User user = users.get(patientUid);
						GroupPatient groupPatient = (GroupPatient)objects[1];
						groupPatient.setPatientUid(patientUid);
						List<GroupPatient> groupPatients = QBuilder.db("basedb").select().from("GroupPatient").where(new Where("groupId=", groupPatient.getGroupId()).and("status = ", STATUS_ENABLE).and("hospital =", groupPatient.getHospital()).and("patientUid=", patientUid)).query().getResultObj();
						if (!isEmpty(groupPatients)) {
							if (user != null) {
								return user.getName();
							}
						}
						Integer groupPatientId = (Integer)DBTools.db("basedb").addItem(groupPatient);
						if (isEmpty(groupPatientId)) {
							if (user != null) {
								return user.getName();
							}
						}
						return null;
					}
				}, i, new Object[]{_uids[i], groupPatient}));
			}
			task.stopTask();
			Object[] results = task.getResult();
			String names = "";
			for (int i = 0; i < results.length; i++) {
				Object obj = results[i];
				if (!isEmpty(obj)) {
					names += obj + ",";
				}
			}
			if (!"".equals(names)) {
				names = names.substring(0, names.length() - 1) + "添加失败或已经存在";
			}
			msg.put("data", names);
		}
		msg = queryGroup(doctorGroup);
	}
	Tools.encryptPrint(out, msg);
%>
